package com.kygl.reaserch.dao.imp;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.kygl.reaserch.dao.IReaserchDao;
import com.kygl.reaserch.vo.ReaserchRegister;
import com.kygl.reaserch.vo.ReaserchKind;
import com.kygl.reaserch.vo.ReaserchLevel;
import com.kygl.reaserch.vo.ReaserchWorkShow;
import com.kygl.teacher.vo.Teacher;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;



public class ReaserchDaoImp extends HibernateDaoSupport implements IReaserchDao{

	public boolean addReaserchRegister(ReaserchRegister reaserchRegister) {
		// TODO Auto-generated method stub
	
		try {
			this.getHibernateTemplate().save(reaserchRegister);
			return true;

		} catch (RuntimeException re) {
			throw re;
		}
		
	}

	public boolean delReaserchRegister(ReaserchRegister t) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().delete(t);
			return true;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public boolean delReaserchRegisterById(int id) {
		// TODO Auto-generated method stub
		try {

			this.getHibernateTemplate().delete(this.getHibernateTemplate().get(
					"com.kygl.reaserch.vo.ReaserchRegister", id));
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public ReaserchRegister findReaserchRegisterById(int id) {
		// TODO Auto-generated method stub
		return (ReaserchRegister) this.getHibernateTemplate().get(
				"com.kygl.reaserch.vo.ReaserchRegister", id);
	}
	
	@SuppressWarnings("unchecked")
	public List<ReaserchRegister> findReaserchRegisterByDW(int deptId, int workId) {
		// TODO Auto-generated method stub
		final String hql = "from ReaserchRegister a where a.deptId="+deptId+"and a.workId="+workId + "" +
				" order by a.level";
		
		HibernateCallback hc = new HibernateCallback() {
			@SuppressWarnings("unchecked")
			public Object doInHibernate(org.hibernate.Session session)
					throws HibernateException, SQLException {
	
				Query query = session.createQuery(hql);
				List<ReaserchRegister> testlist = query.list();
				//List testlist = query.list();
				if (testlist.isEmpty()) {
					 System.out.println("no ReaserchRegister");
				}
//				Iterator it = testlist.iterator();
//				while(it.hasNext()){
//					Object[] obj=(Object[]) it.next(); 
//					int aa = (Integer)(obj[0]);
//					int bb = (Integer)(obj[1]);
//					System.out.println("\naa"+aa+"bb"+bb);
//				}
				return testlist;
			}

		};
		List<ReaserchRegister> list= getHibernateTemplate().executeFind(hc);
		
		return list;
	}

	public boolean modifyReaserchRegister(ReaserchRegister t) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().saveOrUpdate(t);
			return true;
		} catch (RuntimeException re) {

			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<ReaserchWorkShow> findSingleRWByDW(int deptId, int workId)
	{
		//recall
final String hql = "select a.id, c.name, b.levelname, a.name, a.remark, a.LoginDatetime " +
		"from ReaserchRegister a," +
		"ReaserchLevel b,ReaserchKind c" +
		" where a.deptId="+deptId+" and a.workId="+workId + 
		" and a.kind=c.kind and a.level=b.level";
		HibernateCallback hc = new HibernateCallback() {
			@SuppressWarnings("unchecked")
			public Object doInHibernate(org.hibernate.Session session)
					throws HibernateException, SQLException {
	
				Query query = session.createQuery(hql);
				List testlist = query.list();
				List<ReaserchWorkShow> list = new ArrayList<ReaserchWorkShow>();
				if (testlist.isEmpty()) {
					 System.out.println("no ReaserchRegister");
				} else {
					Iterator it = testlist.iterator();
					while(it.hasNext()){
						ReaserchWorkShow rs = new ReaserchWorkShow();
						Object[] obj=(Object[]) it.next(); 
						rs.setId((Integer)(obj[0]));
						rs.setKindname((String)(obj[1]));
						rs.setLevelname((String)(obj[2]));
						rs.setName((String)(obj[3]));
						rs.setRemark((String)(obj[4]));
						rs.setLoginDatetime((Timestamp)(obj[5]));
						list.add(rs);
						//System.out.println("\naa"+aa+"bb"+bb);
					}
				}

				return list;
			}

		};
		//end recall
		List<ReaserchWorkShow> list= getHibernateTemplate().executeFind(hc);
		return list;
	}
	
	public boolean addAuxid(int deptId, int workId, int iRand){
		try {
			String hql = "update ReaserchRegister a set a.auxid="+iRand+" " +
					" where a.deptId="+deptId+" and a.workId = "+workId;
			this.getHibernateTemplate().bulkUpdate(hql);
		} catch (DataAccessException e) {
			
			e.printStackTrace();
		}
		return true;
	} 
	
	@SuppressWarnings("unchecked")
	public List<ReaserchRegister> findByAuxId(int auxid){
		final String hql = "from ReaserchRegister a where a.auxid="+auxid;
		return (this.getHibernateTemplate().find(hql));
		
	}

}
